<?php
class UploadDone extends Action {
	/* (non-PHPdoc)
	 * @see Action::execute()
	 */
	public function execute($params) {
		$I = check_login ( ADMIN_LOGIN_PAGE );
		$tmpdir = TMP_PATH . "plupload";
		$count = irqst ( 'uploader_count', 0 );
		if ($count > 0) {
			@set_time_limit ( 0 );
			$atM = new WebAttachmentModel ();
			$uploader = apply_filter ( 'get_uploader', new PlUploader () ); //得到文件上传器
			
			$data ['create_uid'] = $I->uid;
			$data ['create_time'] = time ();
			for($i = 0; $i < $count; $i ++) {
				$tmpfile = new UploadTmpFile ( $i, $tmpdir );
				$rst = false;
				if ($tmpfile->isuploaded () && $uploader->allowed ( $tmpfile->file_ext )) {
					$rst = $uploader->save ( $tmpfile );
				} else {
					elog ( "文件上传失败: 文件上传不完整或文件类型不允许。" );
				}
				if ($rst !== false) { //保存文件成功
					$data ['type'] = $tmpfile->getType ();
					$data ['mine_type'] = $tmpfile->mimeType;
					$data ['url'] = $rst[0];
					$data ['name'] = $tmpfile->title;
					$data ['ext'] = trim ( $tmpfile->file_ext, '.' );
					$data ['alt_text'] = $tmpfile->alt_text;
					$atM->save ( $data );
					//生成缩略图与添加水印
					if (in_array ( $tmpfile->file_ext, array ('.jpg', '.gif', 'jpeg', 'png' ))) {						
						$imageUtil = new ImageUtil($rst[1]);
						$imageUtil->thumbnail(array(array(80,60),array(280,180)));
					}					
				} else {
					elog ( "文件上传失败: " . $uploader->get_last_error () );
				}
				$tmpfile->delete();
			}
		}
		Response::redirect ( './' );
	}
}